define(function (require, exports, module) {
    var Grid = require("grid");
    var Dialog = require("dialog");

    module.exports = {
        initComponent: function (widget) {
            var grid = new Grid({
                parentNode: ".J-grid",
                model: {
                    url: "api/productbatch/query",
                    params: function () {
                        var subnav = widget.get("subnav");
                        return {
                            seal: subnav.getValue("seal"),
                            fetchProperties: "*,product.pkProduct,product.name"
                        };
                    },
                    columns: [{
                        name: "product.name",
                        label: "产品名称"
                    }, {
                        name: "name",
                        label: "批次名称"
                    }, {
                        name: "description",
                        label: "备注",
                        className: "col-md-6"
                    }, {
                        name: "seal",
                        label: "状态",
                        format: function (value, row) {
                            return value ? "停用" : "启用";
                        }
                    }, {
                        name: "operate",
                        label: "操作",
                        format: "button",
                        formatparams: [{
                            id: "seal",
                            text: "启用",
                            show: function (value,data) {
                                return data.seal;
                            },
                            handler: function (index,data,rowEL) {
                                widget.service.chgSeal({
                                    pkProductBatch: data.pkProductBatch,
                                    seal: false
                                },function(ret){
                                    widget.get("grid").refresh();
                                });
                            }
                        }, {
                            id: "unseal",
                            text: "停用",
                            show: function (value,data) {
                                return !data.seal;
                            },
                            handler: function (index,data,rowEL) {
                                widget.service.chgSeal({
                                    pkProductBatch: data.pkProductBatch,
                                    seal: true
                                },function(ret){
                                    widget.get("grid").refresh();
                                });
                            }
                        }, {
                            id: "edit",
                            icon: "icon-edit",
                            handler: function (index,data,rowEL) {
                                widget.service.ifInUse({
                                    pkProductBatch: data.pkProductBatch
                                },function(flag){
                                    if(flag == true){
                                    	Dialog.alert({
                                            content: "该数据已经被引用，不能修改"
                                        })
                                    }else{
                                    	var form = widget.get("form");
                                        form.reset();
                                        form.setData(data);
                                        widget.toCard();
                                    }
                                });
                            }
                        }, {
                            id: "del",
                            icon: "icon-remove",
                            handler: function (index,data,rowEL) {
                                widget.service.ifInUse({
                                    pkProductBatch: data.pkProductBatch
                                },function(flag) {
                                    if (flag == true) {
                                        Dialog.alert({
                                            content: "该数据已经被引用，不能删除"
                                        })
                                    } else {
                                        widget.service.del(data.pkProductBatch,function(){
                                            widget.get("grid").refresh();
                                        });
                                    }
                                });
                            }
                        }]
                    }]
                }
            });
            widget.set("grid", grid);
        }
    };
});